package fileexplorer.commands;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import fileexplorer.Tools;
import fileexplorer.View;

public class Renommer extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();

		
	    View view = (View) page.findView(View.ID);

	    // ici une popup pour modifier le nom
	    rename(view);

		return null;
	}



	public static void rename(final View view) {
		if(view.getFileObjectSelected().size()==1){
			final String path = view.getFileObjectSelected().get(0).getPath();
			String name = view.getFileObjectSelected().get(0).getNom();
			
	    	final Shell dialog = new Shell(view.shell);
	    	dialog.setLayout(new FillLayout());
	    	dialog.setText(Messages.Renommer_Rename);
	    	
	    	Composite compV = new Composite(dialog, SWT.FILL);
	    	FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
	    	fillLayout.marginHeight = 10;
	    	fillLayout.marginWidth = 10;
	    	fillLayout.spacing = 5;
	    	compV.setLayout(fillLayout);
	    	
	    	Composite compH1 = new Composite(compV, SWT.FILL);
	    	compH1.setLayout(new RowLayout(SWT.HORIZONTAL));
	    	
	    	CLabel lbl = new CLabel(compH1, SWT.NULL);
	    	lbl.setText(Messages.Renommer_RenomerTheFile+name+Messages.Renommer_By);
	    	
	    	final Text nameFile = new Text(compH1, SWT.SINGLE | SWT.BORDER);
	    	nameFile.setText(name);
	    	RowData rowData = new RowData();
	    	rowData.width = 100;
	    	nameFile.setLayoutData(rowData);
	    	
	    	Composite compH2 = new Composite(compV, SWT.FILL);
	    	compH2.setLayout(new FillLayout(SWT.HORIZONTAL));
	    	
	    	Button btnCancel = new Button(compH2, SWT.NONE);
	    	btnCancel.setText(Messages.Renommer_Cancel);
	    	btnCancel.addMouseListener(new MouseListener() {
				@Override public void mouseUp(MouseEvent e) {}
				@Override
				public void mouseDown(MouseEvent e) {
					dialog.close();
				}
				@Override public void mouseDoubleClick(MouseEvent e) {}
			});
	    	
	    	Button btnRename = new Button(compH2, SWT.NONE);
	    	btnRename.setText(Messages.Renommer_Rename);
	    	btnRename.addMouseListener(new MouseListener() {
				@Override public void mouseUp(MouseEvent e) {}
				@Override
				public void mouseDown(MouseEvent e) {
					File source = new File(path);

					String newFile;
					String separator = System.getProperty("file.separator"); //$NON-NLS-1$
					if(source.getParent().endsWith(separator)){
						newFile = source.getParent()+nameFile.getText();
					}else{
						newFile = source.getParent()+separator+nameFile.getText();
					}
					File dest = new File(newFile);
					
					source.renameTo(dest);
					dialog.close();
					
					view.refreshIconView(view.getUrl());
				}
				@Override public void mouseDoubleClick(MouseEvent e) {}
			});
	    	
	    	dialog.pack();
	    	dialog.open();
		}else{
	    	// Message d'Erreur -> faire une popup !
	    	Tools.MessageErreur(view.shell,Messages.Renommer_ErrorOfSelection);
	    }
	}

}
